<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/03/14
// +----------------------------------------------------------------------

namespace app\video\datacache;

use think\facade\Cache;
use think\facade\Debug;
/**
 * 同标签相关视频 缓存层
 * @package app\user_center\model
 */
class VideoPlayLabelRelation{
	
	/**
     * 视频标签缓存
     * @param play_id int 视频id
     * @param cache 是否更新 缓存
     * @author jason wang <120907037@qq.com>
     * @return array
     */
    public function getPlayLable($play_id, $cache = false){
        $cacheName = 'play_lable_arr_'.$play_id;//dump(!Cache::get($cacheName));
        if(!Cache::get($cacheName) || $cache){
            $map = [];
            $map['play_id'] = $play_id;
            $list = model('VideoPlayLabelRelation') -> getListByPlayid($play_id);
            $data = [];
            foreach ($list['data'] as $key => $value) {//dump($value->get_video_play_class_label);
                if(!isset($value->get_video_play_class_label)) continue;
                $data[] = [
                    'id'    => $value->get_video_play_class_label->id,
                    'name'  => $value->get_video_play_class_label->title,
                ]; 
            }
            if(!empty($data)){
                Cache::set($cacheName, $data, 60*60*24*7);//默认缓存7天
			} 
        }
        $result = Cache::get($cacheName) ? Cache::get($cacheName) : [];
        return $result;
	}
	
    /**
     * 根据标签推送相关视频
     * @param user_uid 	int 用户id
     * @param play_id 	int 视频id
     * @param play_type int 视频类型
     * @param page 		int 分页
     * @param page_size int 每页显示数量
     * @param cache 是否更新 缓存
     * @param limit 获取池子大小 默认1000条
     * @author shiqiang
     * @return array
     */
    public function getLabelVideoPushList($user_uid, $play_id, $play_type, $page = 1, $page_size = 10){
        $list = $this -> getLabelPushListByid($play_id, $play_type);
		if(!empty($list)){
			$list_key = array_rand_tt($list, $page_size);
			$lista = [];
			foreach($list_key as $key => $value){
				if(!isset($list[$key]))continue;
				$lista[] = $list[$key];
			}
			$result_list = controller('Videologic', 'logic')->returnBaceArray($user_uid, $lista);
		}else{
			$result_list = [];
		}
        return array_out(200, 'success', $result_list);
    }
	
	/**
     * 获取标签 相关视频  池子
     * @param play_id int 标签id
     * @param cache 是否更新 缓存
     * @author shiqiang
     * @return array
     */
    public function getLabelPushListByid($play_id, $play_type, $cache = false){
        $cacheNameA = 'label_push_play_list_short_id'.$play_id;//短视频
        $cacheNameB = 'label_push_play_list_long_id'.$play_id;//长视频
		
		if($play_type == 1){
			$cacheName = $cacheNameA;
		}else if($play_type == 2){
			$cacheName = $cacheNameB;
		}else{
			return [];
		}
        $cache_list = Cache::get($cacheName);
        if(empty($cache_list) || $cache){
			$label_id = model('VideoPlayLabelRelation') -> where('play_id', $play_id) -> column('label_id');//此视频下所有标签
			//拉取 标签相关 最新1000条 视频id

			$video_classid 	= model('VideoPlay') -> find($play_id) -> video_classid;//视频信息
			$pid_arr 		= model('VideoPlayClass') -> getParentsId($video_classid);//所有父节点 array
			$pid 			= !empty($pid_arr) ? $pid_arr[0] : $video_classid;
			$classid 		= model('VideoPlayClass') -> getChildsId($pid);//子节点
			
			$list = model('VideoPlayLabelRelation') -> group('play_id') -> where([['label_id' , 'in', $label_id],['play_id', 'neq', $play_id]]) -> limit(1000)-> column('play_id');
			$lista = [];
			$listb = [];
			foreach($list as $key => $value){
				$play_id = $value;
				$map = [];
				$map = [
						['id', '=', $value],
						['play_status', '=', 1],
						['play_operation_status', '=', 1],//视频状态 0禁用 1启用 2测试可见
						['video_classid', 'in', $classid],
					];
				
				$play_info 	= model('VideoPlay') -> where($map) -> find();//视频信息
				if(!empty($play_info) && $play_info -> play_type == 1){//短视频
					$lista[] = $play_info -> toArray();
				}
				if(!empty($play_info) && $play_info -> play_type == 2){//长视频
					$listb[] = $play_info -> toArray();
				}
			}
			
			Cache::set($cacheNameA, $lista, 60*60*24);//默认缓存1天
			Cache::set($cacheNameB, $listb, 60*60*24);//默认缓存1天
        }

        return Cache::get($cacheName);
    }
}
